# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import Mean, Mad
from ..base import OpsFeature

from .tp import TP

class CCI(OpsFeature):
    """Technical Indicator - CCI

    - CCI = (TP - MA(TP, N)) / (0.015 * AVEDEV(TP, N))
    """
    def __init__(self, feature=None, N=14):
        self.feature = feature or TP()
        self.N = N

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'tp':
            name += '_'+str(self.feature)
        if self.N != 14:
            name += '_'+str(self.N)
        return name
    
    def _load_internal(self, instrument, start_time, end_time, freq):
        cci = (self.feature - Mean(self.feature, self.N)) / (0.015 * Mad(self.feature, self.N))
        return cci.load(instrument, start_time, end_time, freq)
